/**
 * post controller
 */
var Post = require('../models/post.js');
var Tags = require('../models/tags.js');

var MESSAGES = {
    DATAMISS : '缺少必要数据！',
    NOTLOGIN : '未登录，请登录后再操作！',
    SUCCESS  : '操作成功！',
    FAIL     : '操作失败！请稍后再试~',
    TOOSHORT : '内容太短~多写两个字吧~',
    SCLOSS   : '请填写6位验证码~',
    SCINCORRECT: '验证码不正确~',
    SAYSUCCESS: '发布成功！'
};

exports.say = function(req, res){

    if(!req.session.sid){

        res.json({code: 0, msg: MESSAGES.NOTLOGIN});

    }else if(req.body.content.length < 10){

        res.json({code: 0, msg: MESSAGES.TOOSHORT });

    }
    else if(req.body.sc.length !== 6) {
        res.json({code: 0, msg: MESSAGES.SCLOSS});
    }
    else {
        //验证码验证
        var sc = req.session.sc.toLowerCase();
        var userSc = req.body.sc.toString().toLowerCase();
        if(userSc != sc){
            res.json({code: 0, msg: MESSAGES.SCINCORRECT});
            return false;
        }

        Post.add(req.body, req.session.sid, function(e){

            if(e){
                    res.json({code: 0, msg: e + MESSAGES.FAIL});
            } else {
                if(req.body.tags){
                    Tags.addTags(req.body.tags, function(){
                        res.json({code: 1, msg: MESSAGES.SAYSUCCESS});
                    });
                }else {
                    res.json({code: 1, msg: MESSAGES.SAYSUCCESS});
                }
            }
        });
    }
};

exports.getPosts = function (req, res){


    Post.getPostsCount(function(err, count) {
        if (err) {
            console.log(err);
            res.json({code: 0, msg: err});
        } else if(req.body.home){
            //来自个人主页的请求
            Post.findByUserName(req.body.name, req.body.start, req.body.pageNumber, function (posts, e){
                if(e){
                    console.log(e);
                    res.json({code: 0, msg: e});
                } else {
                    res.json({code: 1, posts: posts, count: count});
                }
            });
        }else {
            //来自首页的请求
            Post.findByPage(req.body.start, req.body.pageNumber, req.body.hot, req.body.tag, function (posts, e){
                if(e){
                    console.log(e);
                    res.json({code: 0, msg: e});
                } else {
                    Tags.findHotTags(function(err, tags){
                        if(err){
                            console.log(err);
                        }
                        res.json({code: 1, posts: posts, tags:tags, count: count});
                    });
                }
            });
        }
    });
};

exports.deletePost = function (req, res) {
    if(req.body.id){
        Post.deletePostById(req.body.id, function (e, numberAffected) {
            if(numberAffected == 1){
                res.json({code: 1, msg: MESSAGES.SUCCESS});
            } else {
                res.json({code: 0, msg: MESSAGES.FAIL });
            }
        });
    } else {
        res.json({code: 0, msg: MESSAGES.DATAMISS});
    }

};

exports.vote = function(req, res){

    var voted = req.cookies.voted ? req.cookies.voted : '';

    if(voted.indexOf(req.body.cid) > 0){

        res.json({code: 0});

    } else {

        Post.vote(req.body.cid, req.body.votes);
        //用cookie存储已经赞过的post._id
        res.cookie('voted', voted + '&'+ req.body.cid, {expire: new Date(Date.now() + 900000)});
        res.json({code: 1});

    }
};

exports.editPost = function (req, res) {
    if(!req.session.sid){
        res.json({code: 0, msg: MESSAGES.NOTLOGIN});
    } else {
        if(req.body.postId && req.body.content){
            Post.updatePostById(req.body, function (e, numberAffected) {
                if(e || numberAffected != 1){;
                    res.json({code: 0, msg: MESSAGES.FAIL});
                } else {
                    res.json({code: 1, msg: MESSAGES.SUCCESS});
                }
            });
        }
    }
};

exports.searchPost = function (key, callback) {
    Post.searchPost(key, function (err, posts) {
        if(err){
            callback(null);
        } else {
            callback(posts);
        }
    })
};

exports.getPostById = function(req, res){
    if(req.body.postId){
        Post.findByPostId(req.body.postId, function (e, post) {
            res.json({code: 1, post: post});
        });
    } else {
        res.json({code: 0, msg: MESSAGES.DATAMISS});
    }
};

exports.deletePostByUserName = function (name, callback) {
    Post.deletePostByUserName(name, callback);
};